package org.detailsmatter.util.version;

import java.io.IOException;
import java.util.Properties;

/**
 * Retrieves version from the classpath : /version.properties.
 * Typically that file will get populated by maven using a build-number plugin.
 * <p/>
 * The properties are read only once, during construction.
 * 
 * @author <a href="mailto:biethb@gmail.com">Bruno Bieth</a>
 */
public class FsProjectVersionProvider implements ProjectVersionProvider {

	private String scmVersion;
	private String mavenVersion;

	public FsProjectVersionProvider() {
		this("/version.properties");
	}
	
	public FsProjectVersionProvider( String versionPropertiesResource ) {
		Properties versionProperties = new Properties();
		try {
			versionProperties.load( getClass().getResourceAsStream( versionPropertiesResource ) );
			scmVersion = versionProperties.getProperty( "version.scm");
			mavenVersion = versionProperties.getProperty( "version.maven");
		} catch( IOException e ) {
			throw new RuntimeException( "Can't load " + versionPropertiesResource, e );
		}
	}
	
	@Override
	public String getMavenVersion() {
		return mavenVersion;
	}

	@Override
	public String getScmVersion() {
		return scmVersion;
	}
	
	/**
	 * Usage
	 */
	public static void main( String[] args ) {
		ProjectVersionProvider versionProvider = new FsProjectVersionProvider();
		System.out.println( "Maven version = " + versionProvider.getMavenVersion() );
		System.out.println( "SCM version = " + versionProvider.getScmVersion() );
	}
}
